using System;
using System.Collections.Generic;
using System.Text;

using System.Data.Common;

using MakeIT.Framework.Data;
using MakeIT.Alf.Site.Entidades;

namespace MakeIT.Alf.Site.Negocios
{
	public partial class AlfDAO : DAO
	{

		public static ImovelxareacomumVO getImovelxareacomum(int? imovelxareacomum, string obs)
		{
			ImovelxareacomumVO imovelxareacomumVO = null;
			List<ImovelxareacomumVO> listImovelxareacomumVOs = getImovelxareacomums(imovelxareacomum, obs, null, null);
			if (listImovelxareacomumVOs != null && listImovelxareacomumVOs.Count > 0)
			{
				imovelxareacomumVO = listImovelxareacomumVOs[0];
			}
			return imovelxareacomumVO;
		}

		public static ImovelxareacomumVO getImovelxareacomum(int? imovelxareacomum)
		{
			return getImovelxareacomum(imovelxareacomum, null);
		}

		public static List<ImovelxareacomumVO> getImovelxareacomums()
		{
			return getImovelxareacomums(null, null, null, null);
		}

		public static List<ImovelxareacomumVO> getImovelxareacomums(int? imovelxareacomum)
		{
			return getImovelxareacomums(imovelxareacomum, null, null, null);
		}

		public static List<ImovelxareacomumVO> getImovelxareacomums(int? imovelxareacomum, string obs)
		{
			return getImovelxareacomums(imovelxareacomum, obs, null, null);
		}

		public static List<ImovelxareacomumVO> getImovelxareacomums(int? imovelxareacomum, string obs, int? idImovel, int? idAreaComum)
		{
			string where = "WHERE ";
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT imoa.idImovel as imoa_idImovel, imo2.titulo as imo2_titulo, imoa.idAreaComum as imoa_idAreaComum, arc.areaComum as arc_areaComum, imoa.obs as imoa_obs, imoa.imovelxareacomum as imoa_imovelxareacomum, imoa.Cadastro as imoa_Cadastro, imoa.CadastradoPor as imoa_CadastradoPor, imoa.Edicao as imoa_Edicao, imoa.EditadoPor as imoa_EditadoPor  ");
			query.AppendLine("FROM imovelxareacomum as imoa LEFT OUTER JOIN imovel as imo2 on imoa.idImovel=imo2.idImovel  LEFT OUTER JOIN areacomum as arc on imoa.idAreaComum=arc.idAreaComum  ");
			if (idImovel != null)
			{
				query.AppendLine(where + "imoa.idImovel=@idImovel ");
				where = "AND ";
			}
			if (idAreaComum != null)
			{
				query.AppendLine(where + "imoa.idAreaComum=@idAreaComum ");
				where = "AND ";
			}
			if (obs != null)
			{
				query.AppendLine(where + "imoa.obs LIKE @obs ");
				where = "AND ";
			}
			if (imovelxareacomum != null)
			{
				query.AppendLine(where + "imoa.imovelxareacomum=@imovelxareacomum ");
				where = "AND ";
			}


			query.AppendLine("ORDER BY imoa.obs ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			if (idImovel!= null) cmd.Parameters.Add(getParameter(cmd, "@idImovel", idImovel));
			if (idAreaComum!= null) cmd.Parameters.Add(getParameter(cmd, "@idAreaComum", idAreaComum));
			if (obs!= null) cmd.Parameters.Add(getParameter(cmd, "@obs", "%"+obs+"%"));
			if (imovelxareacomum!= null) cmd.Parameters.Add(getParameter(cmd, "@imovelxareacomum", imovelxareacomum));


			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();

				List<ImovelxareacomumVO> listImovelxareacomum = new List<ImovelxareacomumVO>();

				while (reader.Read())
				{
					ImovelxareacomumVO imovelxareacomumVO = ImovelxareacomumVO.CreateVO();
					imovelxareacomumVO.idImovel = reader.GetInt32(reader.GetOrdinal("imoa_idImovel"));
					if (!reader.IsDBNull(reader.GetOrdinal("imo2_titulo")))
						imovelxareacomumVO.idImovelVO.titulo = reader.GetString(reader.GetOrdinal("imo2_titulo"));
					imovelxareacomumVO.idAreaComum = reader.GetInt32(reader.GetOrdinal("imoa_idAreaComum"));
					if (!reader.IsDBNull(reader.GetOrdinal("arc_areaComum")))
						imovelxareacomumVO.idAreaComumVO.areaComum = reader.GetString(reader.GetOrdinal("arc_areaComum"));
					if (!reader.IsDBNull(reader.GetOrdinal("imoa_obs")))
						imovelxareacomumVO.obs = reader.GetString(reader.GetOrdinal("imoa_obs"));
					imovelxareacomumVO.imovelxareacomum = reader.GetInt32(reader.GetOrdinal("imoa_imovelxareacomum"));
					imovelxareacomumVO.cadastro = reader.GetDateTime(reader.GetOrdinal("imoa_Cadastro"));
					imovelxareacomumVO.cadastradoPor = reader.GetString(reader.GetOrdinal("imoa_CadastradoPor"));
					imovelxareacomumVO.edicao = reader.GetDateTime(reader.GetOrdinal("imoa_Edicao"));
					imovelxareacomumVO.editadoPor = reader.GetString(reader.GetOrdinal("imoa_EditadoPor"));


					listImovelxareacomum.Add(imovelxareacomumVO);
				}
				reader.Close();
				return listImovelxareacomum;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
                if (cmd != null && cmd.Connection != null)
                    cmd.Connection.Close();
            }
		}

		public static void setImovelxareacomum(ImovelxareacomumVO imovelxareacomumVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("UPDATE imovelxareacomum ");
			query.AppendLine("SET idImovel=@idImovel, idAreaComum=@idAreaComum, obs=@obs,  Cadastro=@Cadastro, CadastradoPor=@CadastradoPor, Edicao=@Edicao, EditadoPor=@EditadoPor ");
			query.AppendLine("WHERE imovelxareacomum=@imovelxareacomum ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@idImovel", imovelxareacomumVO.idImovel));
			cmd.Parameters.Add(getParameter(cmd, "@idAreaComum", imovelxareacomumVO.idAreaComum));
			if (imovelxareacomumVO.obs == null)
				cmd.Parameters.Add(getParameter(cmd, "@obs", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@obs", imovelxareacomumVO.obs));
			cmd.Parameters.Add(getParameter(cmd, "@imovelxareacomum", imovelxareacomumVO.imovelxareacomum));
			cmd.Parameters.Add(getParameter(cmd, "@Cadastro", imovelxareacomumVO.cadastro));
			cmd.Parameters.Add(getParameter(cmd, "@CadastradoPor", imovelxareacomumVO.cadastradoPor));
			cmd.Parameters.Add(getParameter(cmd, "@Edicao", imovelxareacomumVO.edicao));
			cmd.Parameters.Add(getParameter(cmd, "@EditadoPor", imovelxareacomumVO.editadoPor));

			try
			{
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex) { throw ex; }
            finally
            {
                if (cmd != null && cmd.Connection != null)
                    cmd.Connection.Close();
            }

		}

		public static long newImovelxareacomum(ImovelxareacomumVO imovelxareacomumVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("INSERT INTO imovelxareacomum ");
			query.AppendLine("  ( idImovel, idAreaComum, obs, Cadastro, CadastradoPor, Edicao, EditadoPor ) ");
			query.AppendLine("VALUES ( @idImovel, @idAreaComum, @obs, @Cadastro, @CadastradoPor, @Edicao, @EditadoPor ) ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@idImovel", imovelxareacomumVO.idImovel));
			cmd.Parameters.Add(getParameter(cmd, "@idAreaComum", imovelxareacomumVO.idAreaComum));
			if (imovelxareacomumVO.obs == null)
				cmd.Parameters.Add(getParameter(cmd, "@obs", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@obs", imovelxareacomumVO.obs));
			cmd.Parameters.Add(getParameter(cmd, "@Cadastro", imovelxareacomumVO.cadastro));
			cmd.Parameters.Add(getParameter(cmd, "@CadastradoPor", imovelxareacomumVO.cadastradoPor));
			cmd.Parameters.Add(getParameter(cmd, "@Edicao", imovelxareacomumVO.edicao));
			cmd.Parameters.Add(getParameter(cmd, "@EditadoPor", imovelxareacomumVO.editadoPor));


			try
			{
				cmd.ExecuteNonQuery();

				long newID = 0;
				if (tipo.Equals(DBType.MSSql))
				{
					cmd.CommandText = "SELECT IDENT_CURRENT('imovelxareacomum') AS 'Identity' ";
					DbDataReader reader = cmd.ExecuteReader();
					reader.Read();
					newID = (long)reader.GetDecimal(reader.GetOrdinal("Identity"));
					reader.Close();
				}
				else if (tipo.Equals(DBType.MySql))
				{
					newID = ((MySql.Data.MySqlClient.MySqlCommand)cmd).LastInsertedId;
				}
				else if (tipo.Equals(DBType.ODBC))
				{
					//TODO implementar
				}
				else if (tipo.Equals(DBType.ORACLE))
				{
					//TODO implementar
				}
				return newID;
			}
			catch (Exception ex) { throw ex; }
            finally
            {
                if (cmd != null && cmd.Connection != null)
                    cmd.Connection.Close();
            }

		}

		public static void delImovelxareacomum(ImovelxareacomumVO imovelxareacomumVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("DELETE FROM imovelxareacomum ");
			query.AppendLine("WHERE imovelxareacomum=@imovelxareacomum ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@imovelxareacomum", imovelxareacomumVO.imovelxareacomum));


			try
			{
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex) { throw ex; }
            finally
            {
                if (cmd != null && cmd.Connection != null)
                    cmd.Connection.Close();
            }

		}
		public static int getCountImovelxareacomumxIdImovel(int? idImovel)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idImovel) as ocorrencias FROM imovelxareacomum WHERE IdImovel=@IdImovel ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdImovel", idImovel));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
                if (cmd != null && cmd.Connection != null)
                    cmd.Connection.Close();
            }
		}
		public static int getCountImovelxareacomumxIdAreaComum(int? idAreaComum)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idAreaComum) as ocorrencias FROM imovelxareacomum WHERE IdAreaComum=@IdAreaComum ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdAreaComum", idAreaComum));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
                if (cmd != null && cmd.Connection != null)
                    cmd.Connection.Close();
            }
		}
	}
}
